// index.ts
// 获取应用实例
// const app = getApp<IAppOption>()
import msg from "../../utils/msg";
import { getGameList, getSlide,getGrid } from "../../utils/request/api";
import { filePath1 ,url2} from "../../utils/request/route";
import User from "../../utils/user";
Component({
  data: {
    filePath1,//文件地址
    url2,//第二个后端地址
    list:[],
    datas:[],
    datas_user:[],
    swiperPic:[],
    grid:[],
    gridColors:['#DD7666','#FFE699','#66CAA9','#85BDFC'],
    windowHeight:0,
    active:0,
  },
  methods: {
    // onChange(e:any){
    //   this.setData({active:e.detail.index});
    //   if(e.detail.index==1){
    //     this.setData({
    //       list:this.data.datas_user,
    //     })
    //   }
    //   else this.setData({
    //     list:this.data.datas,
    //   })
    // },
    showImgDetail(e: any) {
      let url = e.currentTarget.dataset.url;
      wx.previewImage({
        current: url,
        urls: [url],
      });
    },
    goToMer(){
      wx.navigateTo({
        url:"/pages/mer/mer",
      })
    },
    goToDetail(e:any){
      let index = e.currentTarget.dataset.index;
      let data:any = this.data.datas[index];
      wx.navigateTo({
        url:"/pages/compete/comDetail/comDetail"+'?id='+data.id,
      })
    },
    goToNews(){
      wx.navigateTo({
        url:'/pages/news/news',
      });
    },
    goToCom(){
      wx.switchTab({
        url:'/pages/compete/compete',
      });
    },
    goToAct(){
      wx.switchTab({
        url:'/pages/act/act',
      });
    },
    grid(e:any){
      console.log('e.currentTarget.dataset.index=',e.currentTarget.dataset.index);
      switch(e.currentTarget.dataset.index){
        case 0:this.goToCom();break;
        case 1:this.goToAct();break;
        case 2:this.goToNews();break;
        case 3:this.goToMer();break;
      }
    },
    onShow:async function(){
      msg.Loading();
      let user = new User;
      await getGameList().then((res:any)=>{
        if(res.statusCode==200){
          this.setData({
            datas:res.data.datas,
          });
        } 
        else msg.show('服务器维护中','请稍后再试');
      });
      await getGameList({type:2,visit_gfid:user.GF_ID}).then((res:any)=>{
        msg.hideLoading();
        if(res.statusCode==200){
          this.setData({
            datas_user:res.data.datas,
          });
        } 
        else msg.show('服务器维护中','请稍后再试');
      });
      this.setData({
        list:(this.data.active==0)?this.data.datas:this.data.datas_user,
      });
    },
    onLoad:async function() {
      this.setTopHeight();
      //轮播图获取
      msg.Loading('获取首页轮播图');
      let res_silde:any = await getSlide();
      let res_grid:any = await getGrid();
      console.log('res_grid=',res_grid);
      this.setData({
        swiperPic:res_silde.data,
        grid:res_grid.data,
      });
      msg.hideLoading();
    },
    setTopHeight(){
      let windowHeight = wx.getSystemInfoSync().windowHeight;
      this.setData({
        windowHeight:windowHeight,
      })
    },
  },
})
